/* swap_endian.h */

#ifndef DODO_SWAP_ENDIAN_H
#define DODO_SWAP_ENDIAN_H

#include <algorithm>

namespace dodomet
{

namespace implement
{
	template<int N>
	inline void byte_swap(unsigned char *c)
	{
		std::swap(*c, *(c + N - 1));
		byte_swap<N - 2>(++c);
	}
	template<>
	inline void byte_swap<0>(unsigned char *c)
	{
	}
	template<>
	inline void byte_swap<1>(unsigned char *c)
	{
	}
}

template<typename T>
inline void swap_endian(T& x)
{
	implement::byte_swap<sizeof(T)>(reinterpret_cast<unsigned char*>(&x));
}

}
#endif //DODO_SWAP_ENDIAN_H

